clear
close all
clc

%% Load Data

load('	.mat')
load('TMS_Long.mat')

%% Compute number of CRs in each block

% LONG - blocks of 10 trials
for group=1:3
    for block=1:7
        LONG_BLOCKS{group,block}=[];
    end
end

for group=1:3
    for individual=1:LONG_ngen{group}
        for block=1:7
           LONG_BLOCKS{group,block}=[LONG_BLOCKS{group,block} ...
               sum(LONG_CR{group,individual}(10*(block-1)+1:block*10))/10*100];
        end
    end
end

% LONG_EXP - blocks of 10 trials
for group=1:3
    for block=1:7
        LONG_BLOCKS_EXP{group,block}=[];
    end
end

for group=1:3
    for individual=1:LONG_ngen_exp{group}
        for block=1:7
           LONG_BLOCKS_EXP{group,block}=[LONG_BLOCKS_EXP{group,block} ...
               sum(LONG_CR_EXP{group,individual}(10*(block-1)+1:block*10))/10*100];
        end
    end
end

% SHORT - blocks of 11 trials
for group=1:3
    for block=1:7
        SHORT_BLOCKS{group,block}=[];
    end
end

for group=1:3
    for individual=1:SHORT_ngen{group}
        for block=1:7
           SHORT_BLOCKS{group,block}=[SHORT_BLOCKS{group,block} ...
               sum(SHORT_CR{group,individual}(11*(block-1)+1:block*11))/11*100];
        end
    end
end

% SHORT_EXP - blocks of 11 trials
for group=1:3
    for block=1:7
        SHORT_BLOCKS_EXP{group,block}=[];
    end
end

for group=1:3
    for individual=1:SHORT_ngen_exp{group}
        for block=1:7
           SHORT_BLOCKS_EXP{group,block}=[SHORT_BLOCKS_EXP{group,block} ...
               sum(SHORT_CR_EXP{group,individual}(11*(block-1)+1:block*11))/11*100];
        end
    end
end

%% Figure 1a - LONG

tit={'Session_1','Session_{2sham}','Session_{2tms}'};

figureFullScreen
for group=1:3
    subplot(1,3,group)
    L=[];
    g_L=[];
    for block=1:7
        % Experimental
        L=[L,LONG_BLOCKS_EXP{group,block}];
        g_L = [g_L, repmat({['G' num2str(group) ' B' num2str(block) ' EXP']},...
            size(LONG_BLOCKS_EXP{group,block}))];
        % Simulation
        L=[L,LONG_BLOCKS{group,block}];
        g_L = [g_L, repmat({['G' num2str(group) ' B' num2str(block)]},...
            size(LONG_BLOCKS{group,block}))];
    end
    boxplot(L,g_L)
    set(gca,'fontsize',16)
    xtickangle(45)
    ylim([-10 110])
    title(tit{group},'fontsize',22)
    ylabel('CR%','fontsize',16)
    box off
 end

%% Figure 1b - SHORT

tit={'Session_1','Session_{2sham}','Session_{2tms}'};

figureFullScreen
for group=1:3
    subplot(1,3,group)
    S=[];
    g_S=[];
    for block=1:7
        % Experimental
        S=[S,SHORT_BLOCKS_EXP{group,block}];
        g_S = [g_S, repmat({['G' num2str(group) ' B' num2str(block) ' EXP']},...
            size(SHORT_BLOCKS_EXP{group,block}))];
        % Simulation
        S=[S,SHORT_BLOCKS{group,block}];
        g_S = [g_S, repmat({['G' num2str(group) ' B' num2str(block)]},...
            size(SHORT_BLOCKS{group,block}))];
    end
    boxplot(S,g_S)
    set(gca,'fontsize',16)
    xtickangle(45)
    ylim([-10 110])
    title(tit{group},'fontsize',22)
    ylabel('CR%','fontsize',16)
    box off
end

%% Learn vs Unlearn - PAIRED
% Figure 2

LONG_Learn_S1 = LONG_BLOCKS{1,1};
LONG_Learn_S2_sham = LONG_BLOCKS{2,1};
LONG_Learn_S2_tms = LONG_BLOCKS{3,1};

SHORT_Learn_S1 = SHORT_BLOCKS{1,1};
SHORT_Learn_S2_sham = SHORT_BLOCKS{2,1};
SHORT_Learn_S2_tms = SHORT_BLOCKS{3,1};

LONG_UnLearn_S1 = LONG_BLOCKS{1,7}-LONG_BLOCKS{1,6};
LONG_UnLearn_S2_sham = LONG_BLOCKS{2,7}-LONG_BLOCKS{2,6};
LONG_UnLearn_S2_tms = LONG_BLOCKS{3,7}-LONG_BLOCKS{3,6};

SHORT_UnLearn_S1 = SHORT_BLOCKS{1,7}-SHORT_BLOCKS{1,6};
SHORT_UnLearn_S2_sham = SHORT_BLOCKS{2,7}-SHORT_BLOCKS{2,6};
SHORT_UnLearn_S2_tms = SHORT_BLOCKS{3,7}-SHORT_BLOCKS{3,6};

figureFullScreen
subplot(1,2,1)
boxplot([LONG_Learn_S1,LONG_Learn_S2_sham,LONG_Learn_S2_tms,SHORT_Learn_S1,SHORT_Learn_S2_sham,SHORT_Learn_S2_tms], ...
        [repmat({'Long S1'},size(LONG_Learn_S1)),...
         repmat({'Long S2 sham'},size(LONG_Learn_S2_sham)),...
         repmat({'Long S2 tms'},size(LONG_Learn_S2_tms)),...
         repmat({'Short S1'},size(SHORT_Learn_S1)),...
         repmat({'Short S2 sham'},size(SHORT_Learn_S2_sham)),...
         repmat({'Short S2 tms'},size(SHORT_Learn_S2_tms)),...
        ]);
set(gca,'fontsize',16)
title('Learn','fontsize',22)
xtickangle(45)
ylabel('CR%','fontsize',16)
ylim([0 100])

subplot(1,2,2)
boxplot([LONG_UnLearn_S1,LONG_UnLearn_S2_sham,LONG_UnLearn_S2_tms,SHORT_UnLearn_S1,SHORT_UnLearn_S2_sham,SHORT_UnLearn_S2_tms], ...
        [repmat({'Long S1'},size(LONG_UnLearn_S1)),...
         repmat({'Long S2 sham'},size(LONG_UnLearn_S2_sham)),...
         repmat({'Long S2 tms'},size(LONG_UnLearn_S2_tms)),...
         repmat({'Short S1'},size(SHORT_UnLearn_S1)),...
         repmat({'Short S2 sham'},size(SHORT_UnLearn_S2_sham)),...
         repmat({'Short S2 tms'},size(SHORT_UnLearn_S2_tms)),...
        ]);
set(gca,'fontsize',16)
title('UnLearn','fontsize',22)
xtickangle(45)
ylabel('CR%','fontsize',16)
box off
ylim([-100 0])
%% Change of Parameters
% Figure 3

% LONG
LongSham=LONG_Parameters{2}(:,1:6);
for param=1:6
    [h(param),p(param)]=kstest((LongSham(:,param)-mean(LongSham(:,param)))/std(LongSham(:,param)));
end
disp([num2str(sum(h)) ' parameters are not normally distributed'])

LongTms=LONG_Parameters{3}(:,1:6);
for param=1:6
    [h(param),p(param)]=kstest((LongTms(:,param)-mean(LongTms(:,param)))/std(LongTms(:,param)));
end
disp([num2str(sum(h)) ' parameters are not normally distributed'])

% SHORT
ShortSham=SHORT_Parameters{2}(:,1:6);
for param=1:6
    [h(param),p(param)]=kstest((ShortSham(:,param)-mean(ShortSham(:,param)))/std(ShortSham(:,param)));
end
disp([num2str(sum(h)) ' parameters are not normally distributed'])

ShortTms=SHORT_Parameters{3}(:,1:6);
for param=1:6
    [h(param),p(param)]=kstest((ShortTms(:,param)-mean(ShortTms(:,param)))/std(ShortTms(:,param)));
end
disp([num2str(sum(h)) ' parameters are not normally distributed'])

LongSham_mean=mean(LongSham);
ShortSham_mean=mean(ShortSham);

LongTms=100*(LongTms-LongSham_mean)./LongSham_mean;
ShortTms=100*(ShortTms-ShortSham_mean)./ShortSham_mean;

figureFullScreen
subplot(1,6,1)
boxplot([LongTms(:,1:2)],...
    {'LTP1 Long','LTD1 Long'})
set(gca,'fontsize',16)
title('PF-PC','fontsize',22)
ylabel('Parameter change [%]','fontsize',16)
box off
ylim([-100 100])
hold on
plot([0 3],[0 0],'--k')

subplot(1,6,2)
boxplot([ShortTms(:,1:2)],...
    {'LTP1 Short','LTD1 Short'})
set(gca,'fontsize',16)
title('PF-PC','fontsize',22)
ylabel('Parameter change [%]','fontsize',16)
box off
ylim([-100 100])
hold on
plot([0 3],[0 0],'--k')

subplot(1,6,3)
boxplot([LongTms(:,3:4)],...
    {'LTP2 Long','LTD2 Long'})
set(gca,'fontsize',16)
title('MF-DCN','fontsize',22)
ylabel('Parameter change [%]','fontsize',16)
box off
ylim([-100 100])
hold on
plot([0 3],[0 0],'--k')

subplot(1,6,4)
boxplot([ShortTms(:,3:4)],...
    {'LTP2 Short','LTD2 Short'})
set(gca,'fontsize',16)
title('MF-DCN','fontsize',22)
ylabel('Parameter change [%]','fontsize',16)
box off
ylim([-100 100])
hold on
plot([0 3],[0 0],'--k')

subplot(1,6,5)
boxplot([LongTms(:,5:6)],...
    {'LTP3 Long','LTD3 Long'})
set(gca,'fontsize',16)
title('PC-DCN','fontsize',22)
ylabel('Parameter change [%]','fontsize',16)
box off
ylim([-100 600])
hold on
plot([0 3],[0 0],'--k')

subplot(1,6,6)
boxplot([ShortTms(:,5:6)],...
    {'LTP3 Short','LTD3 Short'})
set(gca,'fontsize',16)
title('PC-DCN','fontsize',22)
ylabel('Parameter change [%]','fontsize',16)
box off
ylim([-100 600])
hold on
plot([0 3],[0 0],'--k')